/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.c
 * Copyright (C) Maxim Ermilov 2009 <zaspire@rambler.ru>
 * 
 * main.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * main.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <config.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
/*
 *!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 *ALL CODE IN JS-APP
 */



/*
 * Standard gettext macros.
 */
#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define textdomain(String) (String)
#  define gettext(String) (String)
#  define dgettext(Domain,Message) (Message)
#  define dcgettext(Domain,Message,Type) (Message)
#  define bindtextdomain(Domain,Directory) (Domain)
#  define _(String) (String)
#  define N_(String) (String)
#endif

gpointer  recv_thread (gpointer data)
{
	int c = (int)data;
	while (1)
	{
		int len;
		if (ioctl (c, FIONREAD, &len) == -1)
		{
			g_error ("Error in ioctl call.");	
		}
		if (len > 4)
		{
			int in;
			gchar *buf;
			if (recv (c, &len, 4, 0) == -1)
			{
				g_error ("Can not recv.");	
			}
			if (len < 0)
			{
				g_error ("Incorrect data recived.");
			}
			buf = g_new (char, len);
			do
			{
				if (ioctl (c, FIONREAD, &in) == -1)
				{
					g_error ("Error in ioctl call.");	
				}
				usleep (20);
			}
			while (in < len);
			if (recv (c, buf, len, 0) == -1)
			{
				g_error ("Can not recv.");	
			}
			buf [len] = '\0';
			puts (buf);
		}
		usleep (20);
	}
}

int
main (int argc, char *argv[])
{
#ifdef ENABLE_NLS
	bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
#endif
	struct sockaddr_in serverAddr;
	struct sockaddr *cp;
	int sock = socket (AF_INET, SOCK_STREAM, 0);
	int *sz;

	if (sock == -1)
	{
		g_error ("Can not create socket.");
	}

	serverAddr.sin_family = AF_INET;
	serverAddr.sin_addr.s_addr= htonl (INADDR_ANY);
	serverAddr.sin_port = htons (5550);

	if (bind (sock, ((struct sockaddr*)&serverAddr), sizeof (serverAddr)) == -1)
	{
		g_error ("Socket bind failed.");
	}
	listen (sock , 5);
	int c;
	int size;
	if ((c = accept(sock, cp, &sz)) == -1)
		g_error ("Can not accept.");
	if (!g_thread_supported ()) g_thread_init (NULL);
	g_thread_create (recv_thread, c, FALSE, NULL);
	int b = 0;
	while (!b)
	{
		char buf[100];
		if (scanf ("%s", buf) != 1)
		{
			exit (0);
		}
		size = strlen (buf);
		if (send(c, &size, 4, 0) == -1)
		{
			g_error ("Can not send.");
		}
		if (send(c, buf, size, 0) == -1)
		{
			g_error ("Can not send.");
		}
	}
	close (c);
	close (sock);
	return 0;
}
